package scuba.DiveBuddy.Fragments.NewLogEntry;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.joda.time.DateTime;
import org.joda.time.LocalTime;
import scuba.DiveBuddy.AirMixBuilderActivity;
import scuba.DiveBuddy.Class.AirMix;
import scuba.DiveBuddy.Class.LogEntries;
import scuba.DiveBuddy.Class.LogPageSanitiser;
import scuba.DiveBuddy.Core.Core;
import scuba.DiveBuddy.Core.Toolbelt;
import scuba.DiveBuddy.Core.Triple;
import scuba.DiveBuddy.Enums.DistanceEnum;
import scuba.DiveBuddy.Enums.DiveTypeEnum;
import scuba.DiveBuddy.Enums.PressureUnitsEnum;
import scuba.DiveBuddy.R;

import java.util.Date;

/**
 * @author Alex Anderson
 * @version 1.0
 * @since {26/02/14}
 */
public class LogProfileFragment extends Fragment
    implements TimePicker.OnTimeChangedListener,
    Spinner.OnItemSelectedListener,
    EditText.OnFocusChangeListener,
    Button.OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_editor_profile, container, false);

        //Setup the UI
        //Spinners
        Spinner s = (Spinner)v.findViewById(R.id.spinDiveType);
        s.setAdapter(new ArrayAdapter<DiveTypeEnum>(getActivity(), R.layout.component_spinneritem, DiveTypeEnum.values()));
        s.setOnItemSelectedListener(this);

        s = (Spinner)v.findViewById(R.id.spinDepthUnits);
        s.setAdapter(new ArrayAdapter<DistanceEnum>(getActivity(), R.layout.component_spinneritem, DistanceEnum.values()));
        s.setOnItemSelectedListener(this);

        s = (Spinner)v.findViewById(R.id.spinAirUnits);
        s.setAdapter(new ArrayAdapter<PressureUnitsEnum>(getActivity(), R.layout.component_spinneritem, PressureUnitsEnum.values()));
        s.setOnItemSelectedListener(this);

        s = (Spinner)v.findViewById(R.id.spinAirMixes);
        s.setAdapter(new ArrayAdapter<AirMix>(getActivity(), R.layout.component_spinneritem, AirMix.GetMixes()));
        s.setOnItemSelectedListener(this);

        //Time pickers
        TimePicker tp = (TimePicker)v.findViewById(R.id.timeInSelect);
        tp.setOnTimeChangedListener(this);
        tp.setIs24HourView(true);

        tp = (TimePicker)v.findViewById(R.id.timeOutSelect);
        tp.setOnTimeChangedListener(this);
        tp.setIs24HourView(true);

        //Text Boxes
        EditText e = (EditText)v.findViewById(R.id.depthSelect);
        e.setOnFocusChangeListener(this);

        e = (EditText)v.findViewById(R.id.startAirSelect);
        e.setOnFocusChangeListener(this);

        e = (EditText)v.findViewById(R.id.endAirSelect);
        e.setOnFocusChangeListener(this);

        //Buttons
        //Button b = (Button)v.findViewById(R.id.btnCreateMix);
        //b.setOnClickListener(this);

        return v;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        if(view == null)
            return;

        int compID = adapterView.getId();
        switch(compID) {
            case R.id.spinDepthUnits:
                Log.v("INTERFACE", "[DEBUG] distanceEnum=" + DiveTypeEnum.values()[i]);
                LogEntries.getCurrent().setDepthUnits(DistanceEnum.values()[i]);
                break;

            case R.id.spinDiveType:
                Log.v("INTERFACE", "[DEBUG] diveType=" + DiveTypeEnum.values()[i]);
                LogEntries.getCurrent().setDiveType(DiveTypeEnum.values()[i]);
                break;

            case R.id.spinAirUnits:
                Log.v("INTERFACE", "[DEBUG] pressureUnits=" + PressureUnitsEnum.values()[i]);
                LogEntries.getCurrent().setAirUnits(PressureUnitsEnum.values()[i]);
                break;

            case R.id.spinAirMixes:
                AirMix selected = AirMix.GetMixes()[i];
                Log.v("INTERFACE", "[DEBUG] airmixID=" + selected.getLongDesc());
                LogEntries.getCurrent().setAirMix(selected);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        //nothing
    }

    @Override
    public void onTimeChanged(TimePicker timePicker, int hour, int min) {
        int id = timePicker.getId();

        LocalTime selectedTime = Toolbelt.Toolkit.createTime(hour, min);
        switch(id) {
            case R.id.timeInSelect:
                if(LogPageSanitiser.getInstance().sanitiseTimeIn(selectedTime)) {
                    LogEntries.getCurrent().setTimeIn(selectedTime);
                    Log.v("INTERFACE", LogEntries.getCurrent().toString());
                }
                else
                    Toolbelt.Toolkit.showToast(getActivity().getApplicationContext(), Core.Errors.TimeInError());

                break;

            case R.id.timeOutSelect:
                if(LogPageSanitiser.getInstance().sanitiseTimeOut(selectedTime)) {
                    LogEntries.getCurrent().setTimeOut(selectedTime);
                    Log.v("INTERFACE", LogEntries.getCurrent().toString());
                }
                else
                    Toolbelt.Toolkit.showToast(getActivity().getApplicationContext(), Core.Errors.TimeOutError());
                break;

            default: break;
        }
    }

    @Override
    public void onFocusChange(View view, boolean b) {

        if(b)
            return;

        int compID = view.getId();
        switch(compID) {
            case R.id.depthSelect:
                Triple<Boolean, Float, String> result = LogPageSanitiser.getInstance().sanitiseDepth(((EditText) view).getText().toString());
                if(result.x) {
                    Log.v("INTERFACE", "User has Closed DepthSelectDialog with depth = " + result.y);
                    LogEntries.getCurrent().setMaxDepth(result.y);
                }
                else {
                    Toolbelt.Toolkit.showToast(getActivity().getApplicationContext(), result.z);
                }

                break;

            case R.id.startAirSelect:
                result = LogPageSanitiser.getInstance().sanitiseStartAir(((EditText) view).getText().toString());
                if(result.x) {
                    Log.v("INTERFACE", "User has Closed startAirSelect with air =" + result.y);
                    LogEntries.getCurrent().setStartAir(result.y);
                }
                else {
                    Toolbelt.Toolkit.showToast(getActivity().getApplicationContext(), result.z);
                }
                break;

            case R.id.endAirSelect:
                result = LogPageSanitiser.getInstance().sanitiseEndAir(((EditText) view).getText().toString());
                if(result.x) {
                    Log.v("INTERFACE", "User has Closed endAirSelect with air =" + result.y);
                    LogEntries.getCurrent().setEndAir(result.y);
                }
                else {
                    Toolbelt.Toolkit.showToast(getActivity().getApplicationContext(), result.z);
                }
                break;
        }
    }

    @Override
    public void onClick(View view) {
        //only one button
        switch(view.getId()) {
            //case R.id.btnCreateMix:
                //Intent i = new Intent(getActivity(), AirMixBuilderActivity.class);
                //getActivity().startActivity(i);
                //break;
        }
    }
}
